﻿using Mrc.Common;
using Mrc.Domain.Entitity;
using Mrc.SqlSurging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Mrc.Domain.Module.ConfigCenter
{
    public class SysConfigService:SqlSurgarClientService,ISysConfigService
    {
        public  SqlSugarClient Db;
        public SysConfigService()
        {
            Db = Client("admin");
        }
        public SysConfigService(ISqlSurgarClientService appSqlSurgarClientService)
        {
            Db= appSqlSurgarClientService.Client("admin");
        }
        public Task Add(SysConfig model)
        {
            Verify(model);
            Db.Ado.UseTran(() =>
            {
                Db.Insertable(model).ExecuteCommand();
            });
            return Task.FromResult(true);
        }
        public Task<bool> Add(List<SysConfig> models)
        {
            models.ForEach((model) => Verify(model));

            Db.Insertable(models).ExecuteCommand();

            return Task.FromResult(true);
        }
        public Task Update(SysConfig model)
        {
            return Task.FromResult(true);
        }
        public Task Delete(string ItemName)
        {
            return Task.FromResult(true);
        }

        public void Verify(SysConfig model)
        {
            if (model.Version == 0)
            {
                throw new MrcException("无效版本");
            }
            if (string.IsNullOrWhiteSpace(model.ItemName))
            {
                throw new MrcException("项目名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(model.FileName))
            {
                throw new MrcException("文件名称不能为空");
            }
            if (model.Env < 0)
            {
                throw new MrcException("环境错误");
            }
            if (string.IsNullOrWhiteSpace(model.KeyName))
            {
                throw new MrcException("存在空键值");
            }

        }
    }
}
